package com.wenjunwu.blog.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.wenjunwu.blog.common.JSONResult;
import com.wenjunwu.blog.common.PageTranslate;
import com.wenjunwu.blog.em.OptionEnum;
import com.wenjunwu.blog.entity.SystemRole;
import com.wenjunwu.blog.service.SystemRoleService;

@Controller
public class SystemRoleController {

	@Autowired
	private SystemRoleService systemRoleService;
	
	@RequestMapping(path= {"/console/toRoles"})
	public String toSystemRoles() {
		return "console/roles";
	}
	
	@ResponseBody
	@RequestMapping(path= {"/console/roles.json"})
	public  PageTranslate< SystemRole> getSystemRolePage(HttpServletRequest request){
		String temp = request.getParameter("draw") == null ? "1" : request.getParameter("draw");
		String paramPageNum = request.getParameter("start") == null ? "1" : request.getParameter("start");
		Integer pageSize = Integer.valueOf(request.getParameter("length") == null ? "10" : request.getParameter("length"));
		int pageNum = (Integer.valueOf(paramPageNum) / pageSize + 1);
		Map<String, Object> params = new HashMap<String, Object>();
		Page<SystemRole> systemRolePage = systemRoleService.getAllRoles(pageNum, pageSize, params);
		PageTranslate<SystemRole> translate = new PageTranslate<SystemRole>();
		List<SystemRole> result = systemRolePage.getResult();
		translate.setData(result);
		translate.setDraw(Long.valueOf(temp));
		translate.setRecordsTotal(systemRolePage.getTotal());
		translate.setRecordsFiltered(systemRolePage.getTotal());
		return translate;
	}
	
	@ResponseBody
	@RequestMapping(path = "console/{option}/{id}", method = RequestMethod.POST)
	public JSONResult freeUser(@PathVariable String option, @PathVariable Long id) {
		SystemRole systemRole =	new SystemRole();
		systemRole.setId(id);
		if(OptionEnum.OPTION_FREEZE.getValue().equalsIgnoreCase(option)) {
			systemRole.setStatus(OptionEnum.OPTION_INVALID.getValue());
			if (systemRoleService.freezeUnFreeze(systemRole)) {
				JSONResult result = new JSONResult();
				result.setResultCode(OptionEnum.OPTION_SUCCESS.getValue());
				result.setResultContent("角色【"+id+"】已成功冻结");
				return result;
			} else {
				JSONResult result = new JSONResult();
				result.setResultCode(OptionEnum.OPTION_FAILURE.getValue());
				result.setResultContent("角色【"+id+"】冻结失败");
				return result;
			}
		}else {
			systemRole.setStatus(OptionEnum.OPTION_INVALID.getValue());
			if ( systemRoleService.freezeUnFreeze(systemRole)) {
				JSONResult result = new JSONResult();
				result.setResultCode(OptionEnum.OPTION_SUCCESS.getValue());
				result.setResultContent("角色【"+id+"】已成功解冻");
				return result;
			} else {
				JSONResult result = new JSONResult();
				result.setResultCode(OptionEnum.OPTION_FAILURE.getValue());
				result.setResultContent("角色【"+id+"】解冻失败");
				return result;
			}
		}
	}
	 
}
